package com.rjyb.student_management_platform.controller;

import com.rjyb.student_management_platform.pojo.Class;
import com.rjyb.student_management_platform.pojo.*;
import com.rjyb.student_management_platform.service.AdminInOpService;
import com.rjyb.student_management_platform.unit.common.DateTime;
import com.rjyb.student_management_platform.unit.common.Page;
import com.rjyb.student_management_platform.unit.common.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("adminInOp")
public class AdminInOpController {
    @Autowired
    AdminInOpService admin;
    @RequestMapping("login")
    public String login(){
        return "admin/login";
    }
    @RequestMapping("toLogin")
    public String toLogin(Admininop admininop, Model model, HttpSession session){

        return admin.toLogin(admininop,model,session);
    }

    @RequestMapping("toindex")
    public String toindex(){
        return "admin/index";
    }


    //查询学生分页内容
    @RequestMapping("selectStudent")
    public String selectStudent(Model model, Page page){
        int pagenum = admin.selpagenum();
        page.setTotalRow(pagenum);
        page.setPageSize(20);
        List<Student> seleallstudent = admin.seleallstudent(page);
        model.addAttribute("seleallstudent",seleallstudent);
        List<Class> seleallclass = admin.seleallclass();
        model.addAttribute("seleallclass",seleallclass);
        List<Teacher> seleallteacher = admin.seleallteacher();
        model.addAttribute("seleallteacher",seleallteacher);

        return "admin/student_table";
    }

    //条件查询学生内容
    @RequestMapping("selectStudent1")
    public String selectStudent1(long cid,long tid,String name,Model model){

            List<Student> selestudet = admin.selestudet(cid,tid,name);
            model.addAttribute("selestudet",selestudet);
            return "admin/student_table_1";

    }

    //添加学生注册学号手机号名字还有班级
    @RequestMapping("AddStudentName")
    public String AddStudentName(Student student,Model model,String cid,HttpSession session){
            Student student1 = admin.ifselectStudent(student);
            if (student1!=null&&!student1.equals("")){
                session.setAttribute("snc","已经注册过该用户");
            }else {
                if (student.getName() != null && !student.getName().equals("")) {
                    //获取当前时间
                    DateTime dateTime = new DateTime();
                    student.setBirthday(dateTime.nowTime().substring(0,11));
                    admin.AddStudentNameClass(student);
                    admin.AddStudentClass(student, cid);
                    session.setAttribute("snc", "注册成功");
                } else {
                    session.setAttribute("snc", "注册失败");
                }
            }
        return "redirect:AddStudentNameClass";
    }

    //添加学生注册学号手机号名字还有班级
    @RequestMapping("AddStudentNameClass")
    public String AddStudentNameClass(Student student,Model model,String cid,HttpSession session){
        String snc = (String) session.getAttribute("snc");
        if(snc!=null&&!snc.equals("")){
            model.addAttribute("snc",snc);
            session.removeAttribute("snc");
        }
        List<Class> list = admin.selectClass();
        model.addAttribute("list",list);
        return "admin/student";

    }

    //添加老师
    @RequestMapping("AddTeacherBooksubject1")
    public String AddTeacherBooksubject1(Model model, Teacher teacher,String cid,HttpSession session) {
            Teacher teacher1 = admin.ifselectTeacher(teacher);
            if (teacher1!=null&&!teacher1.equals("")){
                session.setAttribute("abj","已注册过该用户");
            }else {
                if (teacher.getName() != null && !teacher.getName().equals("")) {
                    admin.AddTeacherBooksubject(teacher);
                    admin.AddTeacherClass(cid, teacher);
                    session.setAttribute("abj", "注册成功");
                } else {
                    session.setAttribute("abj", "注册失败");
                }
            }
        return "redirect:AddTeacherBooksubject";
    }

    //查看老师
    @RequestMapping("AddTeacherBooksubject")
    public String AddTeacherBooksubject(Model model, Teacher teacher,String cid,HttpSession session){
        String abj = (String) session.getAttribute("abj");
        if(abj!=null&&!abj.equals("")){
            model.addAttribute("abj",abj);
            session.removeAttribute("abj");
        }
        List<Class> list = admin.selectClass();
        List<Booksubject> list1 = admin.selectBooksubject();
        model.addAttribute("list1",list1);
        model.addAttribute("list",list);
        return "admin/teacher";
    }

    //添加班级
    @RequestMapping("AddClass1")
    public String AddClass1(Model model,Class c,HttpSession session) {
        if (c.getClassName() != null && !c.getClassName().equals("")) {
            //查看添加班级有没有重复
            Class c1 = admin.ClassQuery(c);
            if(c1!=null){
                session.setAttribute("ac","该班级已添加");
            }else {
                admin.AddClass(c);
                session.setAttribute("ac","添加成功");
            }
        }else {
            session.setAttribute("ac","添加失败");
        }
        return "redirect:AddClass";
    }

    //查看班级
    @RequestMapping("AddClass")
    public String AddClass(Model model,Class c,HttpSession session,Pager pager){
        pager.setTotalRow(admin.selectClassCT());
        String ac = (String) session.getAttribute("ac");
        if(ac!=null&&!ac.equals("")){
            model.addAttribute("ac",ac);
            session.removeAttribute("ac");
        }
        List<Class> list = admin.selectClassTT(pager);
        model.addAttribute("list",list);
        return "admin/selectClass";
    }



    //查所有老师
    @RequestMapping("selectTeacher")
    public String selectTeacher(Model model0, Pager pager){
        pager.setTotalRow(admin.selectAllTeacherC());
        List<HashMap> teacherList=admin.selectAllTeacher(pager);
        List<HashMap> classList=admin.selectAllClass(pager);
        model0.addAttribute("classList",classList);
        model0.addAttribute("teacherList",teacherList);
        return "admin/teacher_table";
    }

    //通过班级查老师
    @RequestMapping("selectTOfName")
    public String   selectTOfName(String className,Model model0, Pager pager){
        pager.setTotalRow(admin.selectTOfNameC(className));


        List<HashMap> teacherList=admin.selectTOfName(className,pager);

        List<HashMap> classList=admin.selectAllClass(pager);
        model0.addAttribute("classList",classList);
        model0.addAttribute("className",className);

        model0.addAttribute("teacherList",teacherList);
        return "admin/teacher_table1";
    }

    //通过老师名字来搜索老师
    @RequestMapping("selectTOfCName")
    public  String selectTOfCName(String teacherName,Model model0,Pager pager){
        pager.setTotalRow(admin.selectTOfTNameC(teacherName));
        List<HashMap> teacherList=admin.selectTOfTName(teacherName,pager);
        List<HashMap> classList=admin.selectAllClass(pager);  model0.addAttribute("classList",classList);
        model0.addAttribute("teacherList",teacherList);
        model0.addAttribute("teacherName",teacherName);
        return "admin/teacher_table2";

    }

    //查询所有作业
    @RequestMapping("selectAllWork")
    public  String selectAllWork(Model model,Pager pager){
        pager.setTotalRow(admin.selectHomeWorkC());
        List<HashMap> list= admin.selectHomeWorkA(pager);
        List<HashMap> classList=admin.selectAllClass(pager);
        model.addAttribute("list",list);

        model.addAttribute("classList",classList);

        return "admin/homeWork_table";
    }

    //查询作业所在班级
    @RequestMapping("selectWorkClassname")
    public String selectWorkClassname(long jid,Model model){
        List<HashMap> list= admin.selectWorkClassname(jid);
        model.addAttribute("list",list);
        return "admin/selectWorkClassName_table";
    }

    //查询作业完成情况
    @RequestMapping("selectWorkComplete")
    public  String selectWorkComplete(long jid,long cid,Model model){

        List<HashMap> list=admin.selectWorkComplete(jid, cid);
//        List<HashMap> list1=admin.selectWorkNoComplete(list,jid);
        model.addAttribute("list",list);
//        model.addAttribute("list1",list1);
        return "admin/selectWorkComplete_table";

    }

    //查看老师
    @RequestMapping("seleTeacherClass")
    public String seleTeacherClass(Model model,HttpSession session){

        String atc = (String) session.getAttribute("atc");
        if(atc!=null&&!atc.equals("")){
            model.addAttribute("atc",atc);
            session.removeAttribute("atc");
        }
        List<Teacher> list = admin.seleteacher();
        List<Class> list1 = admin.selectClass();
        model.addAttribute("list",list);
        model.addAttribute("list1",list1);
        return "admin/addTeacherClass";
    }

    //添加老师班级
    @RequestMapping("addTeacherClass")
    public String addTeacherClass(Teacher teacher,String ttid,String ccid,HttpSession session,Class_Teacher class_teacher){
        if (ttid !=null&&!ttid.equals("")&&ccid!=null&&!ccid.equals("")) {

            long tid = Long.parseLong(ttid.substring(0,ttid.indexOf("-")));
            long cid = Long.parseLong(ccid.substring(0,ccid.indexOf("-")));

            class_teacher.setTid(tid);
            class_teacher.setCid(cid);
            //用来判断老师和学生的关联表里有没有要添加老师和班级的数据
            Class_Teacher class_teacher1 = admin.selectClass_Teacher(class_teacher);
            if (class_teacher1!=null&&!class_teacher1.equals("")){
                session.setAttribute("atc","老师已带了该班级");
            } else {
                teacher.setTid(tid);
                admin.AddTeacherClass(String.valueOf(cid),teacher);
                session.setAttribute("atc", "添加成功");
            }
        }else {
            session.setAttribute("atc", "请选择你要添加老师的班级");
        }
        return "redirect:seleTeacherClass";
    }
    //考勤
    @RequestMapping("toattendance")
    public  String toattendance(Pager pager,Model model){
        pager.setTotalRow(admin.selectClassC());
        List<HashMap> list= admin.selectClassT(pager);

        model.addAttribute("list",list);
        model.addAttribute("classList",admin.selectAllClass(pager));

        return "admin/attendance_table";
    }
    //状态
    @RequestMapping("attendance")
    public  String attendance(long cid,Model model,Pager pager,long tid ){
        pager.setTotalRow(admin.selectAllAttendancdC(cid, pager,tid));
        List<HashMap> list=admin.selectAllAttendancd(cid,pager,tid);
        model.addAttribute("list",list);
        model.addAttribute("tid",tid);
        model.addAttribute("cid",cid);

        return "admin/allAttendance_table";
    }
    //课节
    @RequestMapping("selectClassSection")
    public  String selectClassSection(Integer classsection,Model model,long cid,String atDate,long tid){
        System.out.println("DADADADAD:"+cid);
        System.out.println("dwadawdawda"+classsection);

        if (atDate.length()==0||classsection==0){
            model.addAttribute("cid",cid);
            model.addAttribute("tid",tid);

            model.addAttribute("no","你未添加日期或者课节数，请添加后重试");


            return "admin/allAttendance_table1";
        }else {
            model.addAttribute("cid",cid);
            model.addAttribute("list",admin.selectClassSection(classsection,cid,atDate,tid));
            model.addAttribute("tid",tid);
            return "admin/allAttendance_table1";}


    }
    //通过班级查考勤
    @RequestMapping("selectCOfName")
    public  String selectCOfName(long cid,Model model,Pager p){
        model.addAttribute("classList",admin.selectAllClass(p));
        List<HashMap> list =admin.selectCOfName(cid);
        model.addAttribute("list",list);
        return "admin/attendance_table1";

    }
    //通过老师名字搜索考勤
    @RequestMapping("selectCOfAtt")
    public String selectCOfAtt(String teacherName,Model model,Pager pager){
        model.addAttribute("classList",admin.selectAllClass(pager));
        model.addAttribute("list",admin.selectCOfAtt(teacherName));
        return "admin/attendance_table2";

    }
    //查作业的筛选
    @RequestMapping("selectAllWorkPlus")
    public  String selectAllWorkPlus(long cid,Model model,Pager pager){
        List<HashMap> list=admin.selectAllWorkPlus(cid);
        model.addAttribute("list",list);
        List<HashMap> classList=admin.selectAllClass(pager);
        model.addAttribute("classList",classList);
        return "admin/homeWork_table2";

    }
    //查作业的模糊查询
    @RequestMapping("selectWorkN")
    public  String selectWorkN(String hoName,Model model,Pager pager){
        List<HashMap> list=admin.selectWorkN(hoName);
        model.addAttribute("list",list);
        List<HashMap> classList=admin.selectAllClass(pager);
        model.addAttribute("classList",classList);
        return "admin/homeWork_table3";
    }

    //查看课程
    @RequestMapping("selectBooksubject")
    public String selectBooksubject(Model model,HttpSession session,Pager pager){
        pager.setTotalRow(admin.selectBooksubjectC());
        String atc = (String) session.getAttribute("nobkj");
        if(atc!=null&&!atc.equals("")){
            model.addAttribute("nobkj",atc);
            session.removeAttribute("nobkj");
        }
        List<Booksubject> booksubject = admin.selectBooksubjectT(pager);
        model.addAttribute("list",booksubject);
        return "admin/selectBooksubject";
    }

    //添加课程
    @RequestMapping("AddCourse")
    public String AddCourse(Booksubject booksubject,HttpSession session){
        if (booksubject!=null&&!booksubject.equals("")){
            //查看课程有没有添加
            Booksubject booksubject1 = admin.ViewCourseRegistrations(booksubject);
            if (booksubject1!=null&&!booksubject1.equals("")){
                session.setAttribute("nobkj","不能添加已有的课程");
            }else {
                //添加课程
                admin.AddCourse(booksubject);
                session.setAttribute("nobkj","添加完成");
            }
        }else {
            session.setAttribute("nobjk","请添加你要添加的课程");
        }
        return "redirect:selectBooksubject";
    }

}
